//
//  QLMSerialNameVC.m
//  Xiaomakuaixiu
//
//  Created by 龚碧容 on 2017/11/17.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMSerialNameVC.h"
#import "QLMPeijianListCVCell.h"
#import "QLMCartypeCReView.h"

@interface QLMSerialNameVC ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    QLMCartypeCReView *reusableview;
    QLMPeijianListCVCell *pjCollectionCell;
}

@property (nonatomic,strong) UICollectionView *seriCollectionView;
@property (nonatomic,strong) NSArray *firstLetterArray;

@end

@implementation QLMSerialNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _itemArray=[NSMutableArray array];
    _firstLetterArray=[NSMutableArray array];
    // Do any additional setup after loading the view.
    [self aCreatMainView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
    [self requestSerialData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestSerialData{
    
    NSMutableDictionary *paramerDic=[NSMutableDictionary dictionary];
    [paramerDic setObject:_machineTypeId forKey:@"machineTypeId"];
    [paramerDic setObject:_brandId forKey:@"brandId"];
    
    [self.view makeToastActivity:CSToastPositionCenter];
    [ClientTool postUrlPath:@"/test/equipment/modelList1" withParamers:paramerDic andBody:nil
                    success:^(id responseObject){
                        
                        NSInteger code = [[responseObject objectForKey:@"flag"] integerValue];
                        if (code == 1) {

                            [_itemArray removeAllObjects];
                            _brandDic=responseObject[@"modelList"];
                            
                            NSArray *kArrSort = [_brandDic allKeys];
                            NSArray *resultkArrSort = [kArrSort sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                                return [obj1 compare:obj2 options:NSNumericSearch];
                            }];
                            for (NSString *firstLetterStr in resultkArrSort) {
                                [_itemArray addObject:[_brandDic objectForKey:firstLetterStr]];
                            }
                            
                            [_seriCollectionView reloadData];
                            [self.view hideToastActivity];
                        }else{
                            ETToast(responseObject[@"msg"]);
                        }
                    }failure:^(NSError *error){
                        [self.view hideToastActivity];
                        ETToast(@"服务器连接失败");
                    }
     ];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)aCreatMainView{
    
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor blackColor];
    coverView.alpha = 0.3;
    coverView.userInteractionEnabled=YES;
    [self.view addSubview:coverView];
    
    _leftView = [[UIView alloc] init];
    _leftView.frame = CGRectMake(0,0,60,kScreen_height);
    _leftView.backgroundColor = [UIColor clearColor];
    _leftView.userInteractionEnabled=YES;
    [self.view addSubview:_leftView];
    
    UIView *alertView = [[UIView alloc] init];
    alertView.frame = CGRectMake(60,0,kScreen_width-60,kScreen_height);
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.userInteractionEnabled=YES;
    [self.view addSubview:alertView];
    
    UIView *navView = [[UIView alloc] init];
    navView.frame = CGRectMake(60,0,kScreen_width-60,QLM_StatusBarAndNavigationBarHeight);
    navView.backgroundColor = kNavigationBarColor;
    [self.view addSubview:navView];
    
    _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftButton.frame = CGRectMake(20, 20+IOS11_OR_LATER_SPACE(24), 44, 44);
    [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [_leftButton setBackgroundColor:[UIColor clearColor]];
    _leftButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [navView addSubview:_leftButton];
    
    UILabel *_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(alertView.frame.size.width/2-30,20+IOS11_OR_LATER_SPACE(24),60, 44)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.text=@"系列";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [navView addSubview:_titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    layout.minimumInteritemSpacing = 0;// 垂直方向的间距
    layout.minimumLineSpacing = 0; // 水平方向的间距
    
    _seriCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, QLM_StatusBarAndNavigationBarHeight, alertView.bounds.size.width, kScreen_height-QLM_StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
    _seriCollectionView.delegate=self;
    _seriCollectionView.dataSource=self;
    _seriCollectionView.showsVerticalScrollIndicator=NO;
    _seriCollectionView.backgroundColor=[UIColor whiteColor];
    _seriCollectionView.alwaysBounceVertical = YES;
    _seriCollectionView.collectionViewLayout=layout;
    [_seriCollectionView registerClass:[QLMPeijianListCVCell class] forCellWithReuseIdentifier:@"QLMPeijianListCVCellId"];
    [alertView addSubview:_seriCollectionView];
    
    [_seriCollectionView registerClass:[QLMCartypeCReView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewId32"];
}

#pragma mark - UICollectionView datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_itemArray.count==0) {
        return 0;
    }
    NSArray *tmpArray=_itemArray[section];
    return tmpArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if ([_brandDic allKeys].count==0) {
        return 0;
    }
    
    _firstLetterArray = [[_brandDic allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    return _firstLetterArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreen_width-80-20)/3, 34);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifiew=@"QLMPeijianListCVCellId";
    pjCollectionCell=[collectionView dequeueReusableCellWithReuseIdentifier:identifiew forIndexPath:indexPath];
    
    pjCollectionCell.pjnameLabel.text=[[_itemArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"modelName"];
    
    return pjCollectionCell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 5, 5, 5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout*)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        reusableview= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewId32" forIndexPath:indexPath];
        reusableview.tiplabel.text = _firstLetterArray[indexPath.section];
        return reusableview;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kScreen_width-80, 34);
}

#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *modelNameStr=[[_itemArray[indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"modelName"];
    self.serialBlock(_codeName,modelNameStr,@"",[_itemArray[indexPath.section] objectAtIndex:indexPath.row]);
}

@end
